package com.qzw.security.service.service.impl;

import com.qzw.common.data.BaseMysqlDao;
import com.qzw.common.service.BaseServiceImpl;
import com.qzw.security.api.model.Permission;
import com.qzw.security.api.query.PermissionQuery;
import com.qzw.security.api.service.IPermissionService;
import com.qzw.security.service.dao.MenuDao;
import com.qzw.security.service.dao.PermissionDao;
import com.qzw.security.service.dao.ResourceDao;
import com.qzw.security.service.entity.SysMenu;
import com.qzw.security.service.entity.SysPermission;
import com.qzw.security.service.entity.SysResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Optional;

/**
 * @author ：quziwei
 * @date ：Created in 2020/6/2 11:55 上午
 * @description：实现了
 */
@Service
public class PermissionServiceImpl extends BaseServiceImpl<Permission, SysPermission,Integer, PermissionQuery> implements IPermissionService {
    @Autowired
    private PermissionDao dao;
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private ResourceDao resourceDao;

    @Override
    protected BaseMysqlDao<SysPermission, Integer> getDao() {
        return dao;
    }

    @Override
    protected SysPermission model2Entity(Permission model) {
        SysPermission sysPermission = null;
        if (!ObjectUtils.isEmpty(model)) {
            sysPermission = new SysPermission();
            sysPermission.setId(model.getId());
            sysPermission.setPermission(model.getPermission());
        }
        return sysPermission;
    }

    @Override
    protected Permission entity2model(SysPermission entity) {
        Permission permission = null;
        if (!ObjectUtils.isEmpty(entity)) {
            permission = new Permission();
            permission.setId(entity.getId());
            permission.setPermission(entity.getPermission());
        }
        return permission;
    }

    @Override
    public List<Permission> findPermissionsByMenuId(Integer menuId) {
        Optional<SysMenu> sysMenu = menuDao.findById(menuId);
        return entity2Model(sysMenu.get().getPermissions());
    }

    @Override
    public List<Permission> findPermissionsByResourceId(Integer resourceId) {
        Optional<SysResource> sysResource = resourceDao.findById(resourceId);
        return entity2Model(sysResource.get().getPermissions());
    }
}
